﻿#region Copyright (c) 2008 plexBB Team
/*  Copyright (c) 2008 plexBB Team
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
#endregion

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace PlexBB.Controls.Admin
{
    public abstract class Message : ForumControl
    {
        public event EventHandler BackToPrevClick;

        #region Controls
        protected HtmlControl gMessage;
        protected ITextControl tMessageTitle;
        protected ITextControl tMessageText;
        protected Control cBackToPrev;
        protected IButtonControl bBackToPrev;
        #endregion

        public string MessageTitle
        {
            get { return tMessageTitle.Text; }
            set { tMessageTitle.Text = value; }
        }

        public string MessageText
        {
            get { return tMessageText.Text; }
            set { tMessageText.Text = value; }
        }

        public string BackToPrevUrl
        {
            get
            {
                return (string)ViewState["BackToPrevUrl"] ?? string.Empty;
            }
            set
            {
                ViewState["BackToPrevUrl"] = value;
            }
        }


        public bool ShowBackToPrev
        {
            get { return cBackToPrev.Visible; }
            set { cBackToPrev.Visible = value; }
        }

        public bool Success
        {
            get
            {
                return ViewState["Success"] == null ? true : (bool)ViewState["Success"];
            }
            set
            {
                ViewState["Success"] = value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            bBackToPrev.Click += new EventHandler(bBackToPrev_Click);
        }

        private void bBackToPrev_Click(object sender, EventArgs e)
        {
            if (BackToPrevUrl.Length > 0)
            {
                Response.Redirect(BackToPrevUrl);
            }
            OnBackToPrevClick(e);
        }

        protected virtual void OnBackToPrevClick(EventArgs e)
        {
            if (BackToPrevClick != null)
            {
                BackToPrevClick(this, e);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

//            cBackToPrev.Visible = BackToPrevUrl.Length > 0;

            if (string.IsNullOrEmpty(tMessageTitle.Text))
            {
                tMessageTitle.Text = Lang[Success ? "INFORMATION" : "WARNING"];
            }

            gMessage.AppendAttribute("class", Success ? "successbox" : "errorbox");
        }
    }
}
